package com.shd.edanpei.ui.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shd.edanpei.R;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.base.dialog.DialogUtil;
import com.shd.edanpei.ui.activity.CreateStepActivity;
import com.shd.edanpei.util.ToastUtil;
import com.socks.library.KLog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by mc on 2018/8/10.
 * yang-wen-wen
 */

public class NewOrderView extends LinearLayout {

    @BindView(R.id.image)
    ImageView imageView;
    @BindView(R.id.tv_address)
    TextView tvAddress;

    private int mIndex;
    private int mType;//1：住房订单   2：车辆订单 4：个人信用   5：企业税金
    private Unbinder mUnbinder;
    private Bundle mBundle = new Bundle();

    public NewOrderView(Context context, int index) {
        this(context, null, index);

    }

    public NewOrderView(Context context, @Nullable AttributeSet attrs, int index) {
        this(context, attrs, 0, index);
    }

    public NewOrderView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int index) {
        super(context, attrs, defStyleAttr);
        this.mIndex = index;
        initView();
    }


    private void initView() {
        LayoutInflater.from(getContext()).inflate(R.layout.view_createorder, this);
        mUnbinder = ButterKnife.bind(this);

        if (mIndex == 0) {
            //住房订单
            mType = 2;
            imageView.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.pic_house));
        } else if (mIndex == 1) {
            //车辆订单
            mType = 1;
            imageView.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.pic_car));
        }else if(mIndex==3){
            mType=4;
            imageView.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.geren));
        }else if(mIndex==5){
            imageView.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.qiyeshuiijin));
        }
    }

    private String mAddress;
    private String mAreaCode;

    @OnClick(R.id.tv_address)
    void clickAddress() {
        DialogUtil.addressPick(getContext(), new DialogUtil.AddressListener() {

            @Override
            public void done(String address, String provinceCode, String cityCode, String districtCode) {
                tvAddress.setText(address);
                tvAddress.setTextColor(getResources().getColor(R.color.black));
                mAddress = address;
                mAreaCode = districtCode;
            }
        });
    }

    @OnClick(R.id.tv_create)
    void onCreate() {
        if (TextUtils.isEmpty(mAddress)) {
            ToastUtil.showToast("请选择当前订单所属区域");
        } else {
            mBundle.clear();
            mBundle.putInt(Constant.ORDER_TYPE, mType);
            mBundle.putString("address", mAddress);
            mBundle.putString("areaCode", mAreaCode);
            startActivity(CreateStepActivity.class, mBundle);
        }
    }

    private void startActivity(Class<?> clz, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(getContext(), clz);
        if (null != bundle) {
            intent.putExtras(bundle);
        }
        getContext().startActivity(intent);
    }

    public void clear() {
        KLog.e("解除绑定");
        if (mUnbinder != null) {
            mUnbinder.unbind();
        }
    }
}
